package com.banyou.banyou.picture;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.banyou.banyou.R;
import com.banyou.banyou.adapter.PicAdapter;
import com.banyou.banyou.base.BaseActivity;
import com.banyou.banyou.customviews.ViewPagerFixed;
import com.banyou.banyou.customviews.indicator.ScrollIndicator;
import com.bumptech.glide.Glide;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * ----------Dragon be here!----------/
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃代码无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 * 项目名称：WheelMedical
 * 包名:com.yyxk.wheelmedical.picture
 * 类描述：
 * 创建人：Random
 * 创建时间：10:39
 * 修改人：Random
 * 修改时间：10:39
 * 修改备注：
 */
public class PictureActivity extends BaseActivity<PicturePresenter> implements PictureContract.View {
    @BindView(R.id.picture_viewpager)
    ViewPagerFixed pictureViewpager;
    @BindView(R.id.picture_indicator)
    ScrollIndicator pictureIndicator;
    @BindView(R.id.picture_iv)
    ImageView mImageView;
    private PhotoView mPhotoView;
    private ArrayList<String> mList;
    private ArrayList<PhotoView> mPhotoList;
    private PicAdapter mAdapter;
    private PagerAdapter mPagerAdapter;
    private int mIndex,mPosition;//当前下标
    @Override
    protected PicturePresenter initPresenter() {
        return new PicturePresenter();
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_picture;
    }

    @Override
    protected View initLayoutView() {
        return null;
    }

    @Override
    protected void init() {
        mPresenter.initPresenter(this,new PictureModel());
        ButterKnife.bind(this);
        mPhotoList=new ArrayList<>();
        Intent intent=getIntent();
        Bundle bundle=intent.getBundleExtra("list");
        mList=bundle.getStringArrayList("list");
        initPhotoView();//初始化PhotoView
        mPosition=bundle.getInt("position");
        mIndex=mPosition;//定义下标
        mAdapter= new PicAdapter(mPhotoList, getApplicationContext(),this);
        mPagerAdapter =new PagerAdapter();
        pictureViewpager.setAdapter(mAdapter);
        pictureIndicator.setUpWithViewPager(pictureViewpager);
        pictureViewpager.setCurrentItem(mPosition);
        pictureViewpager.setOffscreenPageLimit(10);
    }

    /**
     * 初始化PhotoView
     */
    private void initPhotoView(){
        for(int i=0;i<mList.size();i++) {
            mPhotoView = new PhotoView(this);
            mPhotoView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            mPhotoView.setMaximumScale(5);
            mPhotoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
                @Override
                public void onPhotoTap(View view, float x, float y) {
                    PictureActivity.this.finish();//点击图片退出Activity
                }
            });
            Glide.with(this).load(mList.get(i)).placeholder(R.drawable.place_holder).error(R.drawable.net_error).into(mPhotoView);
            mPhotoList.add(mPhotoView);
        }
    }

    @Override
    protected void onDestroy() {
        mPhotoList=null;
        mAdapter=null;
        super.onDestroy();
    }

    @Override
    protected String pageName() {
        return "图片页面";
    }

    private class PagerAdapter extends FragmentStatePagerAdapter {

        public PagerAdapter() {
            super(getSupportFragmentManager());
        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public Fragment getItem(int position) {
            return PictureFragment.newFragment(
                    mList.get(position),
                    position == mIndex);
        }

    }
}
